package com.yufubao.framework.result;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.yufubao.framework.enums.CommonExceptionEnum;
import com.yufubao.framework.util.MapUtil;
import lombok.Data;

import java.io.Serializable;
import java.util.Map;

/**
 * 通用的结果返回类
 *
 * @param <T>
 */
@Data
public class ResponseResult<T> implements Serializable {


    private Integer code;

    private String msg;

    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private T data;

    public ResponseResult() {
        this.code = 0;
        this.msg = "success";
        this.data = null;
    }

    public ResponseResult(Integer code, T data) {
        this.code = code;
        this.data = data;
    }

    public ResponseResult(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ResponseResult(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static ResponseResult errorResult(int code, String msg) {
        ResponseResult result = new ResponseResult();
        return result.error(code, msg);
    }

    public static ResponseResult okResult(int code, String msg) {
        ResponseResult result = new ResponseResult();
        return result.ok(code, null, msg);
    }

    public static ResponseResult ok() {
        return new ResponseResult();
    }

    public static ResponseResult ok(CommonExceptionEnum enums) {
        return setAppHttpCodeEnum(enums, enums.getMsg());
    }

    public static ResponseResult okResult(Object data) {
        ResponseResult result = setAppHttpCodeEnum(CommonExceptionEnum.SUCCESS, CommonExceptionEnum.SUCCESS.getMsg());
        if (data != null) {
            result.setData(data);
        }
        return result;
    }

    public static ResponseResult okResultPage(String key, Object value) {
        Map<String, Object> map = MapUtil.builder().put(key, value).build();
        return ResponseResult.okResult(map);
    }

    public static ResponseResult errorResult(CommonExceptionEnum enums) {
        return setAppHttpCodeEnum(enums, enums.getMsg());
    }

    public static ResponseResult errorResult(CommonExceptionEnum enums, String message) {
        return setAppHttpCodeEnum(enums, message);
    }

    public static ResponseResult setAppHttpCodeEnum(CommonExceptionEnum enums) {
        return okResult(enums.getCode(), enums.getMsg());
    }

    private static ResponseResult setAppHttpCodeEnum(CommonExceptionEnum enums, String message) {
        return okResult(enums.getCode(), message);
    }

    public ResponseResult<?> error(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
        return this;
    }

    public ResponseResult<?> ok(Integer code, T data) {
        this.code = code;
        this.data = data;
        return this;
    }

    public ResponseResult<?> ok(Integer code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
        return this;
    }

    public ResponseResult<?> ok(T data) {
        this.data = data;
        return this;
    }
}
